package com.slm.controller;

import com.slm.entity.Student;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 对外暴露。提供调用
 * @author slm
 * @Date 2020/3/21
 */
@RestController
@RequestMapping("/student")
public class StudentConsumerController {

    @Resource
    private RestTemplate restTemplate;

    private final String SERVICE = "http://microservice-student";

    /**
     * 添加或者修改学生信息
     * @param student
     * @return
     */
    @PostMapping(value="/save")
    private boolean save(Student student){
        return restTemplate.postForObject(SERVICE+"/student/save", student, Boolean.class);
    }

    /**
     * 查询学生信息
     * @return
     */
    @SuppressWarnings("unchecked")
    @GetMapping(value="/list")
    public List<Student> list(){
        return restTemplate.getForObject(SERVICE+ "/student/list", List.class);
    }

    /**
     * 根据id查询学生信息
     * @return
     */
    @GetMapping(value="/get/{id}")
    public Student get(@PathVariable("id") Integer id){
        return restTemplate.getForObject(SERVICE+ "/student/get/"+id, Student.class);
    }

    /**
     * 根据id删除学生信息
     * @return
     */
    @GetMapping(value="/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id){
        try{
            restTemplate.getForObject(SERVICE+ "/student/delete/"+id, Boolean.class);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    /**
     * 根据id删除学生信息
     * @return
     */
    @SuppressWarnings("unchecked")
    @GetMapping(value="/getInfo")
    @ResponseBody
    public Map<String,Object> getInfo() {
        return restTemplate.getForObject(SERVICE+"/student/getInfo/", Map.class);
    }


}
